const app = getApp();
var ids=wx.getStorageSync('Uid');
var inputVal = '';
var msgList = [];
var windowWidth = wx.getSystemInfoSync().windowWidth;
var windowHeight = wx.getSystemInfoSync().windowHeight;
var keyHeight = 0;
Page({
 
 /**
  * 页面的初始数据
  */
 data: {
    scrollHeight: '100vh',
    inputBottom: 0
 },
 
 /**
  * 生命周期函数--监听页面加载
  */
 onLoad: function(options) {
    this.data.Uid=options.Uid;
    this.data.Bid=options.Bid;
    this.setData({
        bname:options.Bname
    })
    this.getRes();
 },
 onShow:function(){
    this.getRes();
 },
 getRes:function(){
    wx.request({
        url:`${app.globalData.url}/message/me-talking`,
        data:{
            Uid:this.data.Uid,
            Bid:this.data.Bid
        },
        success:res=>{
            console.log(res.data)
            for(var i=0;i<res.data.length;i++){
                if(res.data[i].Uid==ids){
                    res.data[i].sta=false
                }else{
                    res.data[i].sta=true
                }
            }
            this.setData({
                msgList:res.data
            })
        } 
    })
 },
 focus: function(e) {
    keyHeight = e.detail.height;
    this.setData({
        scrollHeight: (windowHeight - keyHeight) + 'px'
    });
    this.setData({
        toView: 'msg-' + (msgList.length - 1),
        inputBottom: keyHeight + 'px'
    })
 },
 blur: function(e) {
  this.setData({
   scrollHeight: '100vh',
   inputBottom: 0
  })
  this.setData({
   toView: 'msg-' + (msgList.length - 1)
  })
 
 },

 /**
  * 发送点击监听
  */
 sendClick: function(e) {
    const date=new Date();
    const year=date.getFullYear();
    const month=date.getMonth()+1;
    const day=date.getDate();
    const hh=date.getHours();
    const mm=date.getMinutes();
    const ss=date.getSeconds();
    const time=year+'-'+month+'-'+day+' '+hh+':'+''+mm+':'+ss;
    console.log(e.detail.value)
    wx.request({
        url:`${app.globalData.url}/message/me-talking`,
        method:'POST',
        data:{
            Uid:this.data.Uid,
            Bid:this.data.Bid,
            Ttime:time,
            Ttext:e.detail.value
        },
        success:res=>{
            this.setData({
                inputVal:''
            })
           this.getRes();
        }
    })
    
 },
 toBackClick: function() {
    wx.navigateBack({})
 },
 startInter : function(){
    var that = this;
    that.data.inter= setInterval(
        function () {
            that.getRes()
        }, 500);    
  },

  /**
   * 结束定时器
   */
  endInter: function(){
    var that = this;
    that.clearInterval(that.data.inter)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.endInter()
  },
})